package com.mydemo.charging.user.controller;

import com.mydemo.charging.common.pojo.param.ChargeParam;
import com.mydemo.charging.common.pojo.param.VehicleBindParam;
import com.mydemo.charging.common.pojo.vo.BalanceVO;
import com.mydemo.charging.common.pojo.vo.VehicleVO;
import com.mydemo.charging.common.protocol.JsonResult;
import com.mydemo.charging.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 定义所有和车主有关的接口 6
 * 登录
 * 查询余额
 * 充值
 * 查询车辆绑定
 * 绑定 解绑
 */

@Slf4j
@RestController
public class UserController {
    @Autowired
    private UserService userService;

    @GetMapping("/user/wx/login")
    public JsonResult<Integer> wxLogin(String nickName, String code){
        log.debug("用户开始登录:{},code:{}",nickName,code);
        Integer userId = userService.wxLogin(nickName, code);

        return JsonResult.ok(userId);
    }

    @GetMapping("/user/balance/{userId}")
    public JsonResult<BalanceVO> getBalance(@PathVariable Integer userId){
        log.debug("用户查询余额:{}",userId);
        BalanceVO balanceVO = userService.getBalance(userId);

        return JsonResult.ok(balanceVO);
    }

    @PostMapping("/user/charge")
    public JsonResult charge(@RequestBody ChargeParam param){
        log.debug("用户充值:{}",param);
        userService.charge(param);

        return JsonResult.ok(null);
    }

    @GetMapping("/user/vehicle/binded/{userId}")
    public JsonResult<VehicleVO> bindedVehicle(@PathVariable Integer userId){
        log.debug("用户查询绑定车辆:{}",userId);
        VehicleVO vehicleVO = userService.bindedVehicle(userId);

        return JsonResult.ok(vehicleVO);
    }

    @PostMapping("/user/vehicle/bind")
    public JsonResult bindVehicle(@RequestBody VehicleBindParam param){
        log.debug("用户绑定车辆:{}",param);
        userService.bindVehicle(param);

        return JsonResult.ok(null);
    }
}
